<template>
  <div>
    <el-button type="success" @click.prevent="handleAdd()"
      ><i class="el-icon-circle-plus-outline"></i>新增</el-button
    >
    <el-table :data="questionList" tooltip-effect="dark" style="width: 100%">
      <el-table-column label="编号" prop="id" width="100"> </el-table-column>
      <el-table-column prop="stem" label="题干" width="100"> </el-table-column>
      <el-table-column prop="questionType.qtname" label="试题类型" width="100">
      </el-table-column>
      <el-table-column prop="difficulty.dname" label="难度" width="100">
      </el-table-column>
      <el-table-column prop="subject.sname" label="学科" width="100">
      </el-table-column>
      <el-table-column prop="stage.stname" label="阶段" width="100">
      </el-table-column>
      <el-table-column prop="optionA" label="选项A" width="100">
      </el-table-column>
      <el-table-column prop="optionB" label="选项B" width="100">
      </el-table-column>
      <el-table-column prop="optionC" label="选项C" width="100">
      </el-table-column>
      <el-table-column prop="optionD" label="选项D" width="100">
      </el-table-column>
      <el-table-column prop="optionE" label="选项E" width="100">
      </el-table-column>
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleView(scope.row)"
            >详情</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageNum"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
      style="margin-left: 300px"
    >
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "QuestionList",
  data() {
    return {
      questionList: [],
      id: 0,
      dname: null,
      score: "",
      currentPage: 1,
      pageNum: 5,
      total: 0,
    };
  },
  methods: {
    handleSizeChange(val) {
      this.pageNum = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getList();
    },
    getList() {
      this.axiosJSON
        .get(
          "/question/getAll?currentPage=" +
            this.currentPage +
            "&pageNum=" +
            this.pageNum
        )
        .then((result) => {
          this.questionList = result.data.list;
          this.total = result.data.total;
          for (let i = 0; i < this.questionList.length; i++) {
            if (this.questionList[i].stem.length > 10)
              this.questionList[i].stem =
                this.questionList[i].stem.substring(0, 10) + "...";
          }
        });
    },
    selectQuestion(id) {
      //点击TR事件处理
      this.id = id;
      this.$parent.id = id; //辅助给父组件的数据属性
    },
    checkSelected(id) {
      //检查id是否被选中
      return this.id == id;
    },
    // 查看详情
    handleView(row) {
      this.$router.push("/question/view/" + row.id);
    },
    // 删除
    handleDelete(row) {
      this.$confirm("确定要删除吗？", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.axiosJSON.get("/question/delete?id=" + row.id).then((result) => {
            if (result.data.code == 0) {
              this.$message({
                type: "success",
                message: result.data.msg,
              });
              //如何刷新子组件的数据,使用事件发送机制
              this.oaevent.$emit("updateQuestionList");
              this.getList();
            } else {
              this.$message.error(result.data.msg);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    handleAdd() {
      this.$router.push("/question/add");
    },
  },
  mounted() {
    this.$parent.questionTitle = "试题管理";
    this.$parent.id = 0;
    this.oaevent.$on("updateQuestionList", () => {
      this.getList();
    }); //响应updateQuestionList事件
    this.getList();
  },
};
</script>

<style>
</style>
